Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Objects and Classes in Java

    This page will teach you about Java objects and classes. We design a programme using objects and classes in object-oriented programming.
    In Java, an object is both a physical and logical entity, whereas a class is only a logical entity.

    The classes and objects in Java, together with their characteristics and methods, are the foundation of OOPs. For example, a car is an item in the actual world. The car contains characteristics like weight and colour, as well as functions like drive,brake and acceleration.


Table Of Content

  • Objects and Classes in Java
  • What is an object in Java
  • Three ways to initialize object in Java
  • What is Class in Java






What is an object in Java

  • A member (also known as an instance) of a Java class is a Java object. Every object has a state, a behaviour, and an identity.
  • In Java, the word "new" is used to create an object.
  • The items we can see in the actual world and Java objects are extremely similar. An item can be a pet, a lighter, a pen, or an automobile.
  • Object has 3 characteristics:
    • State:: State represents an object's data (value).
    • Behavior : Behavior reflects how a thing behaves (or how it functions), such as when you deposit or withdraw money from your bank account. its own.
    • Identity :Usually, a distinct ID is used to implement an object's identification. The external user cannot see the value of the ID. However, the JVM uses it internally to uniquely identify each object its own.
  • For Example, Car is an object. Its name is Nano; color is red, known as its state. It is used to drive, so drive is its behavior.





Java objects' properties

  • A thing in the actual world is an object
  • An entity at runtime is an object.
  • An entity with state and behaviour is an object.
  • The object is a class instance.


  • Usually, an object's methods may be used to communicate with it. The objects continue to have control over how the world may utilise them through states and methods.
  • Individual objects of code may be created, and each object's source code can be developed and maintained separately from the others. It is simple to remove and replace any item that interferes with a program's functionality.

  • Syntax

    The following syntax is used to create a new object of type "Student":

    Student StudentObject= New Tree = Student().



Object structure




 // Java  program using class and object   
class Student2{  
	String Stu_name ;   
	int Stu_id;   
} 
class Student{  
	public static void main(String args[]){  
          Student2 studentObject=new Student2();  
          
          studentObject.Stu_id=40139522;  
          
          studentObject.Stu_name="Dr Alok Raja";  
          
    	  System.out.println(studentObject.Stu_name+" \n"+studentObject.Stu_id);
	 } 
} 



Output:

Dr Alok Raja  
40139522 


Different ways to create an object in Java

  • By new keyword
  • By newInstance() method
  • By deserialization
  • By clone() method
  • By factory method
  • We will learn how to make these objects later.





There are 3 ways to initialize object in Java.

  • Using reference variable
  • Using constructor
  • Using method


1.Object initialized through reference variable

 // Java  program using class and object   
class Student2{  
	String Stu_name ;   
	int Stu_id;   
} 
class Student{  
	public static void main(String args[]){  
          Student2 studentObject=new Student2();  
          
          studentObject.Stu_id=40139522;  
          
          studentObject.Stu_name="Dr Alok Raja";  
          
    	  System.out.println(studentObject.Stu_name+" \n"+studentObject.Stu_id);
	 } 
} 



Output:

Dr Alok Raja  
40139522 

2.Object initialized Using constructor

 // Java  program using class and object   
class Student2{  
	String Stu_name ;   
	int Stu_id;
	public Student2(String stu_name, int stu_id) {
		Stu_name = stu_name;
		Stu_id = stu_id;
	}   
} 
class Student{  
	public static void main(String args[]){  
		  Student2 studentObject=new Student2("Dr Alok Raja", 40139522);  
		  System.out.println(studentObject.Stu_name+" \n"+studentObject.Stu_id);
	 } 
}  
 



Output:

Dr Alok Raja  
40139522 

3.Object initialized Using Method

 // Java  program using class and object   
class Student2{  
	String Stu_name ;   
	int Stu_id;
	 void insert(String stu_name, int stu_id) {
		Stu_name = stu_name;
		Stu_id = stu_id;
	}  
	public void Display() {
		System.out.println(Stu_id);
		System.out.println(Stu_name+"\n");
	}
} 
class Student{  
	public static void main(String args[]){  
		  Student2 studentObject1=new Student2();
		  Student2 studentObject2=new Student2();
		  studentObject1.insert("Dr Alok Raja ", 40139522);
		  studentObject1.Display();
		  studentObject2.insert("Mrs Anita Sinha", 40139522);
		  studentObject2.Display();
	 } 
}  



Output:

40139522  
Dr Alok Raja 
  
40139532 
Mrs Anita Sinha 




What is Class in Java

  • In object-oriented programming (OOPs), Classes serves as a tailored blueprint, a precise model used for creating object A class is essentially a blueprint or template that defines the properties and behaviors of objects. OOP classes lassencapsulates both data (in the form of variables) and methods that operate on that data.
  • The class can be compared to a rough draught (prototype) of a home. It includes all of the information on the floors, doors, windows, etc. We construct the house in accordance with these descriptions. The item is a house.
  • We may produce several objects from a class since numerous houses can be constructed using the same description.
  • In Java, a class can include:
    • Fields:
    • Methods
    • Constructors
    • Blocks
    • interface
    • Nested class




Class structure

  • Here, fields (variables) represent the state of object and methods represent the behavior of object
  • fields are used to store data
  • Methods are used to perform some operations
  •  // Java  program using class and object   
    class Car {	   
        public int gear = 2; // state 
      
        public void braking()  // behavior 
        {
          System.out.println("Car braks Working");
        }
    }
      



  • In the preceding example, we defined a class called Car. It has a gear field and a braking method (). Car is a prototype in this context. We can now use the prototype to build an unlimited number of Car. Furthermore, all of the Car will use the prototype's fields and methods.
Java method Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.